package main

import (
	"backenddev/no2/gin_project1/model"
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	/**
	http://localhost:8080/get/John
	**/
	// r.GET("/get/:user_name", func(c *gin.Context) {
	// 	userName := c.Param("user_name")
	// 	println("userName:", userName)
	// 	c.String(http.StatusOK, "处理 GET 请求")
	// })
	/**
	http://localhost:8080/get?user_name=John
	**/
	r.GET("/get", func(c *gin.Context) {
		id := c.Query("id")
		userName := c.Query("user_name")
		println("id:", id)
		println("userName:", userName)
		c.String(http.StatusOK, "处理 GET 请求")
	})
	/**
	**/
	r.POST("/post", func(c *gin.Context) {
		email := c.PostForm("email")
		phone := c.PostForm("phone")
		println(email, phone)
		c.String(http.StatusOK, "处理 GET 请求")
	})

	r.POST("/login", func(c *gin.Context) {
		user := &model.User{}
		err := c.ShouldBind(&user)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		}
		println(user.Name, user.Age, user.Email)
		c.JSON(http.StatusOK, gin.H{"user": user})
	})

	r.Run(":8080")
}
